import pandas as pd
import statsmodels.api as sm
import matplotlib.pyplot as plt
import pylab as pl
import numpy as np

df = pd.read_csv("F:\\test.txt")


print df.head()
# Survived,Age,Sex,classes
# -1.87,-0.228,0.521,-1.0
# -0.923,-0.228,-1.92,1.0
# -0.923,-0.228,-1.92,1.0
# 0.965,-0.228,0.521,1.0


df.columns = ["Survived", "Age", "Sex", "classes"]
print df.columns



print df.describe()


print df.std()


print pd.crosstab(df['classes'], df['Sex'], rownames=['classes'])



df.hist()
plt.show()





cols_to_keep = ["Survived", "Age", "Sex", "classes"]
data = df[cols_to_keep]
data["classes"]=(df["classes"]+1)/2
print data.head()



data['intercept'] = 1.0


train_cols = data.columns[:-1]


logit = sm.Logit(data['classes'], data[train_cols])

result = logit.fit()


import copy
combos = copy.deepcopy(data)


predict_cols = combos.columns[:-1]


combos['intercept'] = 1.0


combos['predict'] = result.predict(combos[predict_cols])


total = 0
hit = 0
for value in combos.values:
    predict = value[-1]
    classes = int(value[-2])

    if predict > 0.5:
        total += 1
        if classes == 1:
            hit += 1

print 'Total: %d, Hit: %d, Precision: %.2f' % (total, hit, 100.0*hit/total)